/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.componenteditor;

import com.chasekernan.hxnova.core.components.Hull;
import org.aswing.geom.IntRectangle;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JAdjuster;
import org.aswing.BorderLayout;
import org.aswing.geom.IntDimension;
import org.aswing.geom.IntPoint;
import com.chasekernan.hxnova.core.components.ComponentTypeSet;
import com.chasekernan.hxnova.utils.Rectangle;
import com.chasekernan.hxnova.utils.Vector;

/**
    A dragable representation of a hull storage space.
**/
class StorageSpaceEditor extends HullEditorObject<StorageSpace> {
    
    /**
        The maximum amount of storage space.
    **/
    public static var MAXIMUM_STORAGE = 10000;
    
    /**
        Creates a new storage space editor with the storage given.
    **/
    public function new(parent : Dynamic, storage : StorageSpace,
						bounds : IntRectangle) {
        super(parent, storage);
        
        centerLabel.setText("Storage");
        adjuster.setMaximum(MAXIMUM_STORAGE);
		
		loadFromObject(storage, bounds);
    }
    
    /**
        Loads the data from the given object.
    **/
    override public function loadFromObject(object : StorageSpace, 
											bounds : IntRectangle) {
        super.loadFromObject(object, bounds);
        
        setLocationXY(Std.int(data.position.x + bounds.x), 
					  Std.int(data.position.y + bounds.y));
        setSize(new IntDimension(Std.int(data.position.width), 
                                 Std.int(data.position.height)));
        adjuster.setValue(data.amount);
    }
    
    /**
        Saves the position and storage space to the object stored by this 
        editor.
        This should be called either every frame or when the hull is saved.
    **/
    override public function saveToObject(bounds : IntRectangle) {
        data.amount = adjuster.getValue();
        
        var location = getLocation();
        data.position.x = location.x - bounds.x;
        data.position.y = location.y - bounds.y;
        
        data.position.width = getWidth();
        data.position.height = getHeight();
    }
    
    /**
        Creates a new storage space object with default settings.
    **/
    public static function createStorageSpace() : StorageSpace {
        return {amount : 100, 
                position : new Rectangle(HullEditorObject.DEFAULT_LOCATION, 
                                         HullEditorObject.DEFAULT_DIMENSIONS)};
    }
    
}